import { IActor } from "../Interface/State"
import { Singleton } from "../Singleton"
import { StateTypeEnum } from "../common/Enum"
import { AttributeManager } from "./AttributeManager"
import { GameLogic } from "./GameLogic"

/** 技能管理器 */
export class HPManager extends Singleton {

    static get Ins() {
        return super.GetInstance<HPManager>()
    }


    /** 自然恢复的生命值 */
    getRegenerationHP() {
        const attr = AttributeManager.Ins.getAttr()
        const hp = GameLogic.Ins.state.actors[0].hp
        let regenerationHP = attr.regeneration
        const diffHp = attr.hpMax - hp
        if (diffHp < attr.regeneration) {
            regenerationHP = diffHp
        }
        return regenerationHP
    }


    /** 玩家扣血 */
    actorReduceHP(actor: IActor, damage: number) {
        const attr = AttributeManager.Ins.getAttr()
        const defense = attr.defense
        let reduceHP = damage * defense / (defense + 100)
        if (defense < 0) {
            reduceHP = damage * (1 + defense / (defense - 100))
        }
        if (reduceHP <= 1) {
            actor.hp -= 1
        } else {
            actor.hp -= Math.ceil(reduceHP)
        }
        if (actor.hp <= 0) {

            ++actor.deadNum
            if (actor.deadNum <= attr.rebornNum) {
                actor.hp = attr.hpMax
            } else {
                actor.state = StateTypeEnum.dead
            }
        }
    }
}